<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
   
	var itemsArray = [];

   $(document).ready(function(){	<? if(!isset($_GET['edit'])){?>
	$("#paginas").hide();
	<? } ?>
	
	$("#createpage").change(function(){
		if($("#createpage").val() == '1'){
			$("#paginas").show();
		}
		else{
			var itemsArray = [];
			$("#paginas").hide();
		}
	})
	
	$("#page").change(function(){
		var keuzeId = $("#page").val();
		var keuzeName = $("#page option:selected").text();
			itemsArray.push(keuzeId);
			
			$("#page option[value='"+keuzeId+"']").remove();
			$("#items").append('<div id="'+keuzeId+'"><img src="../template/images/back/delete.png" width="18" border="0" onclick="remove(\''+keuzeName+'\',\''+keuzeId+'\')">'+keuzeName+'<br/></div>');
			$("#arrayVal").val(itemsArray);
	}) 
	
	remove = function remove(name, id){
		$("#"+id).remove();
		$("#page").append('<option value="'+id+'">'+name+'</option>');
		itemsArray = jQuery.grep(itemsArray, function(value) {
        	return value != id;
     	});
	  	$("#arrayVal").val(itemsArray);
	}

  });
</script>
<section id="main" class="column" >
	<?
	if($this->result == true){
		echo '<h4 class="alert_success">Groep is succesvol toegevoegd.</h4>';
	}
	if($this->updateResult == true)
	{
		echo '<h4 class="alert_success">Groep is succesvol aangepast.</h4>';
	}
	if(isset($_GET['remove'])){
		echo '<h4 class="alert_success">Groep is verwijderd</h4>';
	}
	if(isset($_GET['edit'])){
		echo '
		<article class="module width_full" style="float:left; width:64%">
		<header>
			<h3>Groep bewerken</h3>
		</header>
		<div class="module_content">
        	<fieldset>
            	<form method="post" action="'.$this->baseUrl().'/administrator/groups?update='.$this->group_content->id.'">
                	<input type="hidden" value="" name="arrayVal" id="arrayVal"/>
                    <h3>Groep naam</h3>
                    <input type="text" style="150px" name="name" value="'.$this->group_content->name.'">
                    <h3>Rechten</h3>
                    Administrator
                    <select name="admin">
                        <option value="0"';if($this->group_content->admin == 0){ echo ' selected="selected"';} echo '>Nee</option>
                        <option value="1"';if($this->group_content->admin == 1){ echo ' selected="selected"';} echo '>Ja</option>
                    </select><br/>
                   Pagina Bewerken
                    <select name="createpage" id="createpage" style=" margin-top: 10px;">
                        <option value="0"';if($this->group_content->createpage == 0){ echo ' selected="selected"';} echo '>Nee</option>
                        <option value="1"';if($this->group_content->createpage == 1){ echo ' selected="selected"';} echo '>Ja</option>
                    </select><br>
                    <div id="paginas">
                        Mag pagina\'s aanpassen:<br />
                        <div id="items">';
							$query = mysql_query("SELECT * FROM editpage WHERE group_id = '".$_GET['edit']."'");
							while($data = mysql_fetch_object($query)){
								//echo $data->group_id .' page: '.$data->page_id.'<br>';
								$pagename = mysql_fetch_object(mysql_query("SELECT name FROM page WHERE id = '".$data->page_id."'"));
							
							print '<div id="'.$data->page_id.'">
									<img src="../template/images/back/delete.png" width="18" border="0" onclick="remove(\'naam\',\''.$data->page_id.'\')">
										'.$pagename->name.'<br/>
									</div>
									<script type="text/javascript">
										itemsArray.push(\''.$data->page_id.'\');
									</script>
									';
									
							}
						echo '
						</div>
                        <select name="page" id="page">
                            <option>---Selecteer---</option>';
                            foreach($this->menu as $page){
                                print '<option value="'.$page->id.'">'.$page->name.'</option>';
                            }
                            echo '
                        </select><br />
                   	</div>
                    <input type="submit" value="Verander groep" name="submitEditGroup">
              	</form>
          	</fieldset>
        </div>
	</article>
    ';
	}else{
	?>
	<article class="module width_full" style="float:left; width:64%">
		<header>
			<h3>Groep toevoegen</h3>
		</header>
		<div class="module_content">
        	<fieldset>
            	<form method="post" action="<? echo $this->baseUrl();?>/administrator/groups">
                	<input type="hidden" value="" name="arrayVal" id="arrayVal"/>
                    <h3>Groep naam</h3>
                    <input type="text" style="150px" name="name">
                    <h3>Rechten</h3>
                    Administrator:        
                    <select name="admin">
                        <option value="0" selected="selected">Nee</option>
                        <option value="1">Ja</option>
                    </select><br/>
                    Pagina bewerken: 
                    <select name="createpage" id="createpage" ">
                        <option value="0" selected="selected">Nee</option>
                        <option value="1">Ja</option>
                    </select><br>
                    <div id="paginas">
                        Mag pagina's aanpassen:<br />
                        <div id="items"></div>
                        <select name="page" id="page">
                            <option>---Selecteer---</option>
                            <?
                            foreach($this->menu as $page){
                                print '<option value="'.$page->id.'">'.$page->name.'</option>';
                            }
                            ?>
                        </select><br />
                   	</div>
                    <input type="submit" value="Maak groep" name="submitCreateGroup" style="margin-top: 20px;">
              	</form>
          	</fieldset>
        </div>
	</article>
    <?
	}
	?>
    <article class="module width_quarter" style="float:left; style="overflow: hidden;">
    	<header><h3>Bestaande groepen</h3></header>
        <div class="module_content" style="overflow: hidden;">
			<?php
				foreach ($this->groups as $group) {
					print "<fieldset class='fieldannounce' style='width:98%;'>
							<a href='".$this->baseUrl()."/administrator/groups?edit=".$group->id."'>".$group->name."</a>
							<a href='".$this->baseUrl()."/administrator/groups?remove=".$group->id."'>
						   		<img class='removeBtn' style='float:right;' src='../template/images/back/close.png' alt='deletebutton' />
							</a>
					       </fieldset>";
				}
			?>
        </div>
   	</article>
</section>